package com.shermanzhou.springbootflowable.demo;

import com.shermanzhou.springbootflowable.demo.custom.mapper.CustomActQueryMapper;
import com.shermanzhou.springbootflowable.demo.custom.mapper.CustomActUpdateMapper;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.common.impl.EngineDeployer;
import org.flowable.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;
import org.flowable.engine.impl.rules.RulesDeployer;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * @author zhouchaoqun02
 * @date 2020-06-12 10:18
 */
@Configuration
public class FlowableConfig {

    @Primary
    @Bean(name = "processEngineConfiguration")
    public ProcessEngineConfiguration getSpringProcessEngineConfiguration(@Qualifier("dataSource") DataSource dataSource, @Qualifier("transactionManager") DataSourceTransactionManager transactionManager) {
        SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
        configuration.setDataSource(dataSource);
        configuration.setTransactionManager(transactionManager);
        configuration.setDatabaseSchemaUpdate("false");
        configuration.setAsyncExecutorActivate(true);
        configuration.setCustomPostDeployers(new ArrayList<EngineDeployer>(){
            private static final long serialVersionUID = 4041439225480991716L;
            {
                add(new RulesDeployer());
            }
        });
        //自定义sql
        Set<Class<?>> customMybatisMappers = new HashSet<>();
        customMybatisMappers.add(CustomActQueryMapper.class);
        customMybatisMappers.add(CustomActUpdateMapper.class);
        configuration.setCustomMybatisMappers(customMybatisMappers);
        return configuration;
    }

    @Primary
    @Bean("normalProcessEngine")
    public ProcessEngine processEngine(@Qualifier("processEngineConfiguration") ProcessEngineConfiguration springProcessEngineConfiguration) {
        return springProcessEngineConfiguration.buildProcessEngine();
    }

    @Bean(name = "memProcessEngineConfiguration")
    public ProcessEngineConfiguration getMemProcessEngineConfiguration() {
        StandaloneInMemProcessEngineConfiguration configuration = new StandaloneInMemProcessEngineConfiguration();
        //自定义sql
        Set<Class<?>> customMybatisMappers = new HashSet<>();
        customMybatisMappers.add(CustomActQueryMapper.class);
        customMybatisMappers.add(CustomActUpdateMapper.class);
        configuration.setCustomMybatisMappers(customMybatisMappers);
        return configuration;
    }

    @Bean("simulateProcessEngine")
    public ProcessEngine simulateProcessEngine(@Qualifier("memProcessEngineConfiguration") ProcessEngineConfiguration configuration) {
        return configuration.buildProcessEngine();
    }
}
